package com.smart.imagecoupon.controller;

import com.smart.imagecoupon.bean.BaiduRep;
import com.smart.imagecoupon.bean.RepMessage;
import com.smart.imagecoupon.service.MessageService;
import com.smart.imagecoupon.service.SampleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping(path = "/image")
@Slf4j
public class ImageController {

    @Autowired
    private SampleService sampleService;

    @Autowired
    private MessageService messageService;

    @PostMapping(path = "/identify")
    public RepMessage identify(@RequestParam("image") MultipartFile imageFile) throws IOException {
        log.info(imageFile.getOriginalFilename());
        byte[] imgData = imageFile.getBytes();
        String imageMessage = sampleService.getImageMessage(imgData);
        BaiduRep baiduRep = sampleService.getBaiduReq(imageMessage);
        String name = messageService.getCoupon(baiduRep);
        return messageService.getRepMessage(name);
    }

}
